package util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Scanner;

public class InputUtil {
    private static final Scanner scanner = new Scanner(System.in);
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    public static String readString(String prompt) {
        System.out.print(prompt);
        return scanner.nextLine().trim();
    }

    public static String readNonEmptyString(String prompt) {
        String input;
        do {
            input = readString(prompt);
            if (input.isEmpty()) {
                System.out.println("输入不能为空，请重新输入！");
            }
        } while (input.isEmpty());
        return input;
    }

    public static int readInt(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                return Integer.parseInt(scanner.nextLine().trim());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的整数！");
            }
        }
    }

    public static int readIntRange(String prompt, int min, int max) {
        int input;
        do {
            input = readInt(prompt);
            if (input < min || input > max) {
                System.out.println("请输入" + min + "到" + max + "之间的整数！");
            }
        } while (input < min || input > max);
        return input;
    }

    public static double readDouble(String prompt) {
        while (true) {
            try {
                System.out.print(prompt);
                return Double.parseDouble(scanner.nextLine().trim());
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字！");
            }
        }
    }

    public static double readDoubleRange(String prompt, double min, double max) {
        double input;
        do {
            input = readDouble(prompt);
            if (input < min || input > max) {
                System.out.println("请输入" + min + "到" + max + "之间的数字！");
            }
        } while (input < min || input > max);
        return input;
    }

    public static boolean readBoolean(String prompt) {
        System.out.print(prompt + " (y/n): ");
        String input = scanner.nextLine().trim().toLowerCase();
        return input.equals("y") || input.equals("yes") || input.equals("是") || input.equals("true");
    }

    public static LocalDate readDate(String prompt) {
        while (true) {
            try {
                System.out.print(prompt + " (格式：yyyy-MM-dd): ");
                return LocalDate.parse(scanner.nextLine().trim(), DATE_FORMATTER);
            } catch (DateTimeParseException e) {
                System.out.println("日期格式不正确，请按照yyyy-MM-dd格式重新输入！");
            }
        }
    }

    public static LocalTime readTime(String prompt) {
        while (true) {
            try {
                System.out.print(prompt + " (格式：HH:mm): ");
                return LocalTime.parse(scanner.nextLine().trim(), TIME_FORMATTER);
            } catch (DateTimeParseException e) {
                System.out.println("时间格式不正确，请按照HH:mm格式重新输入！");
            }
        }
    }

    public static LocalDateTime readDateTime(String prompt) {
        while (true) {
            try {
                System.out.print(prompt + " (格式：yyyy-MM-dd HH:mm): ");
                return LocalDateTime.parse(scanner.nextLine().trim(), DATE_TIME_FORMATTER);
            } catch (DateTimeParseException e) {
                System.out.println("日期时间格式不正确，请按照yyyy-MM-dd HH:mm格式重新输入！");
            }
        }
    }
    
    public static void pressEnterToContinue() {
        System.out.print("按回车键继续...");
        scanner.nextLine();
    }
} 